package com.shjn.squirrel.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.shjn.squirrel.R;

/**
 * 通用警告对话框
 */
public class WarningDialog extends BaseDialogFragment implements View.OnClickListener {

    private static WarningDialog instance;
    private Thread mThread;

    public WarningDialog() {
    }

    public synchronized static WarningDialog getInstance() {
        if (instance == null) {
            instance = new WarningDialog();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //设置对话框资源文件
        setLayoutResource(R.layout.warning_dialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void onResume() {
        //设置对话框大小
        setdialogSize(247, 200, -43);
        //初始化控件
        ((Button) mDialog.findViewById(R.id.btn_ok)).setOnClickListener(this);
        TextView message = (TextView) mDialog.findViewById(R.id.tv_tip_message);
        if (mMessgae.equals("投递时间为8:30-21:00哦~")) {
            SpannableString spannableString = new SpannableString(mMessgae);
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#038FFF")),
                    5, spannableString.length() - 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            message.setText(spannableString);
        } else {
            message.setText(mMessgae);
        }
        //默认5秒自动关闭对话框
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mDialog.cancel();
                        mDialog.dismiss();
                        if (mOnCloseDialogListener != null) {
                            mOnCloseDialogListener.onCloseDialog();
                        }
                    }
                });
            }
        });
        mThread.start();
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //确定
            case R.id.btn_ok:
                if (mThread != null) {
                    closeDialog();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 关闭对话框
     */
    public void closeDialog() {
        if (mThread != null) {
            mThread.interrupt();
        }
    }

}
